package com.liao.quart.controller;

import com.liao.quart.annotate.EntityAno;
import com.liao.quart.entity.from.QuartzJobFrom;
import com.liao.quart.entity.from.QuartzLogFrom;
import com.liao.quart.service.QuartzJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/v1/jobs")
public class QuartzJobController {

    @Autowired
    private QuartzJobService quartzJobService;

    public static Map<String, String> map() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("msg", "成功");
        return hashMap;
    }

    @PostMapping("/add")
    public Object add(@Validated @RequestBody QuartzJobFrom from) throws Exception {
        quartzJobService.add(from);
        return new ResponseEntity<Map>(map(), HttpStatus.OK);
    }

    @PostMapping("/update")
    public Object update(@Validated(EntityAno.Update.class) @RequestBody QuartzJobFrom from) throws Exception {
        quartzJobService.updateJob(from);
        return new ResponseEntity<Map>(map(), HttpStatus.OK);
    }

    @PostMapping("/query")
    public Object query(@RequestBody QuartzJobFrom from) throws Exception {
        return quartzJobService.queryList(from);
    }

    @PostMapping("/getById")
    public Object getById(@Validated(value = EntityAno.GetById.class) @RequestBody QuartzJobFrom from) throws Exception {
        return new ResponseEntity<Map>(map(), HttpStatus.OK);
    }

    @PostMapping(value = "/exec/{id}")
    public Object executionQuartzJob(@PathVariable("id") Long id) throws Exception {
        quartzJobService.execution(quartzJobService.getById(id));
        return new ResponseEntity<Map>(map(), HttpStatus.OK);
    }

    @PostMapping(value = "/status/{id}")
    public Object updateQuartzJobStatus(@PathVariable("id") Long id) throws Exception {
        quartzJobService.updateIsEnable(quartzJobService.getById(id));
        return new ResponseEntity<Map>(map(), HttpStatus.OK);
    }

    @PostMapping("/mask")
    public Object mask(@Validated(EntityAno.Mask.class) @RequestBody QuartzJobFrom from) throws Exception {
        quartzJobService.delete(from);
        return new ResponseEntity<Map>(map(), HttpStatus.OK);
    }

    @PostMapping(value = "/logs")
    public Object queryQuartzJobLog(@RequestBody QuartzLogFrom quartzLogFrom) throws Exception {
        return quartzJobService.queryAllLog(quartzLogFrom);
    }
}
